#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n;
char s[N];
int ans;
int main() {
  cin >> n >> s + 1;
  if (n % 2 == 1) {
    cout << "-1\n";
    return 0;
  }
  int left = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '(')
      left++;
    else
      left--;
  }
  if (left != 0) {
    cout << "-1\n";
    return 0;
  }

  for (int i = 1; i <= n; ++i) {
    if (s[i] == ')') {
      left++;
    } else {
      left--;
      if (left >= 0) ans++;
    }
  }
  cout << 2 * ans << endl;
}
